
package com.trydofor.id.doer.shell;

import java.util.Map;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public final class ArgChecker {
    
    private ArgChecker() {
    }
    
    /**
     * check the arguments
     * 
     * @param command the command
     * @param argdata the arguments
     * @return null means okay, otherwise the error.
     */
    public static String check(Command command, Map<String, String> argdata) {
        StringBuilder errput = new StringBuilder();
        
        if (command == null) {
            errput.append("command is null");
        }
        else {
            for (ArgHelp help : command.getArgumentAll()) {
                // validate
                String data = argdata.get(help.getName());
                String err = help.validate(data);
                if (err != null) {
                    errput.append("arg[" + help.getName() + "]" + err + ".\n");
                }
            }
        }
        
        return errput.length() == 0 ? null : errput.toString();
    }
}
